package ro.adventist.devotin2013;


import java.io.IOException;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Locale;

import ro.adventist.devotin2013.R;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Devo_main extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_devo_main);
        SQLiteDatabase myDB= null;
        DataBaseHelper myDbHelper = new DataBaseHelper(this);
        try {        	 
        	myDbHelper.createDataBase();
        	myDB = myDbHelper.openDataBase();
        } catch (IOException ioe) {
        	throw new Error("Unable to create database");
        } catch (SQLException e) {
			e.printStackTrace();
		} 

        
        Cursor c_daily = myDB.rawQuery("SELECT * FROM devotional where date('now') = date(ziua);",  null);
        if(c_daily != null && c_daily.getCount() == 0){
	        TextView v_date = (TextView) findViewById(R.id.day);
	        v_date.setText(DateUtil.getStringFromDate(Calendar.getInstance(new Locale("ro")).getTime()));
        	Button btn_next=(Button)findViewById(R.id.next_button); 
        	btn_next.setVisibility(View.GONE);
        	Button btn_back=(Button)findViewById(R.id.back_button); 
        	btn_back.setVisibility(View.GONE);
        }
        if(c_daily!=null && c_daily.getCount() !=0){
        	Button btn=(Button)findViewById(R.id.next_button); 
        	btn.setEnabled(false);
			int Column1 = c_daily.getColumnIndex("_id");
			int Column2 = c_daily.getColumnIndex("ziua");
			int Column3 = c_daily.getColumnIndex("dilema");
			int Column4 = c_daily.getColumnIndex("verset");
			int Column5 = c_daily.getColumnIndex("autor");
			int Column6 = c_daily.getColumnIndex("mesaj");
			int Column7 = c_daily.getColumnIndex("titlu");
			int Column8 = c_daily.getColumnIndex("citat_sp");
			String date="", title="", verset="", autor="", messageDevo="", id="", titlu="", citat_sp="";
			
			c_daily.moveToFirst();
				do{
					id=c_daily.getString(Column1);
					date=c_daily.getString(Column2);
					title=c_daily.getString(Column3);
					verset=c_daily.getString(Column4);
					autor=c_daily.getString(Column5);
					messageDevo=c_daily.getString(Column6);
					titlu=c_daily.getString(Column7);
					citat_sp=c_daily.getString(Column8);
				}while(c_daily.moveToNext());
	        
	        TextView v_date = (TextView) findViewById(R.id.day);
	        v_date.setText(DateUtil.getStringFromDate(DateUtil.getDateFromString(date)));
	        
	        TextView v_title = (TextView) findViewById(R.id.dilema);
	        v_title.setText(title);
	        
	        TextView v_verset = (TextView) findViewById(R.id.verset);
	        v_verset.setText(verset);
	        v_verset.setTextColor(Color.RED);

	        TextView v_autor = (TextView) findViewById(R.id.autor);
	        v_autor.setText(autor);
	        
	        TextView v_mesaj = (TextView) findViewById(R.id.mesaj);
	        v_mesaj.setText(messageDevo);

	        TextView v_titlu = (TextView) findViewById(R.id.titlu);
	        v_titlu.setText(titlu);
	        
	        TextView v_citat_sp = (TextView) findViewById(R.id.citat_sp);
	        v_citat_sp.setText(citat_sp);	        
	        TextView v_id = (TextView) findViewById(R.id.hidden_value);
	        v_id.setText(id);
	        }
	        myDB.close();
	        c_daily.close();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_devo_main, menu);
		return true;
	}
	
	public void goBack(View view){
		Intent intent = new Intent(this, NavigateActivity.class);
		TextView v_id = (TextView) findViewById(R.id.hidden_value);
		String id = v_id.getText().toString();
		intent.putExtra("id", id);
		startActivity(intent);
		finish();
	}
	
	public void goNext(View view){
		Intent intent = new Intent(this, NavigateActivity.class);
		TextView v_id = (TextView) findViewById(R.id.hidden_value);
		String id = v_id.getText().toString();
		int id_next = Integer.valueOf(id).intValue() + 2;
		intent.putExtra("id", String.valueOf(id_next));
		startActivity(intent);
		finish();
	}
	
    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        finish();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}	

}
